package com.example.servies;

import com.example.feign.ItemClient;
import com.example.feign.UserClient;
import com.tedu.pojo.Item;
import com.tedu.pojo.Order;
import com.tedu.pojo.User;
import com.tedu.service.OrderService;
import com.tedu.utll.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    //注入feign接口
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;

    @Override
    public Order getOrder(String orderId) {
        //TODO: 调用user-service获取用户信息
        //TODO: 调用item-service获取商品信息

        JsonResult<List<Item>> items = itemClient.getItems(orderId);
        JsonResult<User> user = userClient.getUser(8);
        Order order = new Order();
        order.setId(orderId);
        order.setItems(items.getData());
        order.setUser(user.getData());

        return order;
    }

    @Override
    public void addOrder(Order order) {
        //TODO: 调用item-service减少商品库存   远程调用
        itemClient.decreaseNumber(order.getItems());

        //TODO: 调用user-service增加用户积分
        userClient.addScore(order.getUser().getId(),2000);
        log.info("保存订单："+order);
    }
}
